#include "genderdelegate.h"

#include <QComboBox>
#include <QDebug>
#include "systemcontroller.h"

GenderDelegate::GenderDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

/*!
  \fn createEditor
  Ovverides the function the generate the Editor
  */
QWidget *GenderDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *cmbBox = new QComboBox(parent);
    cmbBox->addItem(SystemController::tranlsateGender(true), "male");
    cmbBox->addItem(SystemController::tranlsateGender(false), "female");
    return cmbBox;
}

/*!
    \fn setEditorData
    Sets the Data of the Editor
  */
void GenderDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QComboBox *cmbBox = static_cast<QComboBox*>(editor);
    QString value = index.model()->data(index, Qt::EditRole).toString();

    int maleId, femaleId;

    //Get the Index Values
    if(cmbBox->itemData(0)=="male")
    {
        maleId = 0;
        femaleId = 1;
    }
    else
    {
        maleId=1;
        femaleId = 0;
    }

    if (value=="male")
        cmbBox->setCurrentIndex(maleId);
    else
        cmbBox->setCurrentIndex(femaleId);
}

/*!
  \fn setModelData
  Functions to set the Data from the Editor
  */
void GenderDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *cmbBox = static_cast<QComboBox*>(editor);
    QString value = cmbBox->itemData(cmbBox->currentIndex()).toString();

    model->setData(index, value);
}

/*!
  \fn updateEditorGeometry
  Set the size
  */
void GenderDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}





